<template>
  <a-modal
    :title="initData.pageType === 'edit' ? '编辑人员证书' : ' 新增人员证书'"
    cancelText="取消"
    okText="保存"
    :width="900"
    :visible.sync="visible"
    @ok="handleOk"
    @cancel="cancel"
    :maskClosable="false"
  >
    <a-form-model
      ref="form"
      :model="formInfo"
      :rules="rules"
      :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
      :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
    >
      <a-row>
        <a-col span="12">
          <a-form-model-item label="证书类型" prop="certificateType">
            <a-select placeholder="请选择" v-model="formInfo.certificateType" labelInValue>
              <a-select-option :disabled="!i.enabled" v-for="i in selectList" :key="i.id" :value="i.id">
                {{ i.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
<!--        <a-col span="12">-->
<!--          <a-form-model-item label="证书名称" prop="name">-->
<!--            <BaseInput placeholder="请输入" v-model.trim="formInfo.name" />-->
<!--          </a-form-model-item>-->
<!--        </a-col>-->
        <a-col span="12">
          <a-form-model-item label="证书编号" prop="certificateNo">
            <BaseInput placeholder="请输入" v-model.trim="formInfo.certificateNo" />
          </a-form-model-item>
        </a-col>
        <a-col span="12">
          <a-form-model-item label="发证日期" prop="issueDate">
            <a-date-picker value-format="YYYY-MM-DD" placeholder="请选择" v-model="formInfo.issueDate" />
          </a-form-model-item>
        </a-col>
        <a-col span="12">
          <a-form-model-item label="有效期止" prop="validityDate">
            <a-date-picker value-format="YYYY-MM-DD" placeholder="请选择" v-model="formInfo.validityDate" />
          </a-form-model-item>
        </a-col>
        <a-col span="24">
          <a-form-model-item label="备注">
            <BaseTextarea placeholder="请输入" v-model.trim="formInfo.remarks" />
          </a-form-model-item>
        </a-col>
        <a-col span="24">
          <a-form-model-item label="附件">
            <upload-list
              :businessCode="'SHE08'"
              :uploadArrys.sync="formInfo.fileInfoList"
              multiple
            ></upload-list>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-modal>
</template>

<script>
import { getSelectListByType } from '@/api/safetySupervision/common.js'
import UploadList from '@/components/UploadList'

export default {
  name: 'a' + Date.now(),
  components: {
    UploadList
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    initData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  watch: {
    visible(e) {
      this.formInfo = this.$clone(this.initData)
      if (!this.formInfo.certificateType && this.formInfo.certificateTypeId) {
        this.formInfo.certificateType = {
          key: this.formInfo.certificateTypeId,
          label: this.formInfo.certificateTypeName
        }
      }
    }
  },
  data() {
    return {
      formInfo: {},
      selectList: [],
      rules: {
        certificateType: [{ required: true, message: '请选择', trigger: 'change' }],
        // name: [{ required: true, message: '请输入', trigger: 'change' }],
        certificateNo: [{ required: true, message: '请输入', trigger: 'change' }],
        issueDate: [{ required: true, message: '请选择', trigger: 'change' }],
        validityDate: [{ required: true, message: '请选择', trigger: 'change' }],
        // remarks: [{ required: true, message: '请输入', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getSelectList('26')
  },
  methods: {
    async getSelectList(type) {
      const [err, res] = await getSelectListByType({ type })
      if (!err) {
        this.selectList = res.data || []
      }
    },
    async handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formInfo.certificateTypeName = this.formInfo.certificateType.label
          this.formInfo.certificateTypeId = this.formInfo.certificateType.key
          this.$emit('confirm', this.formInfo)
          this.cancel()
        } else {
          this.$noticeWarning('请填写完整')
        }
      })
    },
    cancel() {
      this.$refs.form.clearValidate()
      this.$emit('update:visible', false)
    }
  }
}
</script>